/*
 * @Author: your name
 * @Date: 2021-02-28 18:26:09
 * @LastEditTime: 2022-01-11 17:33:13
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \nodeAPI\util\dbconfig.js
 */
const mysql = require("mysql");
module.exports = {
  config: {
    host: "localhost",
    port: "3306",
    user: "root",
    password: "root",
    database: "demoapp",
  },
  sqlConnect: function (sql, sqlArr, callBack) {
    var pool = mysql.createPool(this.config);
    pool.getConnection((err, conn) => {
      if (err) {
        console.log(err);
        console.log("连接失败");
        return;
      }
      //事件驱动回调
      conn.query(sql, sqlArr, callBack);
      //释放连接
      conn.release();
    });
  },
  SySqlConnect: function (sySql, sqlArr) {
    return new Promise((resolve, reject) => {
      var pool = mysql.createPool(this.config);
      pool.getConnection(function (err, conn) {
        if (err) {
          reject(err);
        } else {
          conn.query(sySql, sqlArr, (err, data) => {
            if (err) {
              reject(err);
            } else {
              resolve(data);
            }
            conn.release();
          });
        }
      });
    }).catch((err) => {
      console.log(err);
    });
  },
  query(sql) {
    var pool = mysql.createPool(this.config);
    return new Promise((resolve, reject) => {
      pool.getConnection((err, conn) => {
        //掏出错误
        if (err) {
          reject(err);
          return;
        }
        conn.query(sql, (err, res) => {
          if (err) {
            reject(err);
          }
          resolve(res);
          conn.release();
        });
      });
    });
  },
  test() {
    console.log("学习分支合并");
  },
  test3() {
    console.log("学习分支合并3");
  },
  test4() {
    console.log("学习分支合并4");
  },
};
